//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.DataLayer
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Text;

    /// <summary>
    /// Model for exchanging the data between the External registry and Apllication.
    /// </summary>
    [Serializable]
    public class DataObject : IDisposable
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="DataObject"/> class.
        /// </summary>
        /// <param name="stream">The stream.</param>
        public DataObject(Stream stream)
        {
            this.Data = stream;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataObject"/> class.
        /// </summary>
        /// <param name="DataBytes">The data bytes.</param>
        public DataObject(byte[] DataBytes)
        {
            this.DataBytes = DataBytes;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataObject"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        public DataObject(string value)
        {
            this.DataBytes = UnicodeEncoding.Unicode.GetBytes(value);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataObject"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        public DataObject(Uri value)
        {
            this.DataBytes = UnicodeEncoding.Unicode.GetBytes(value.ToString());
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataObject"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        public DataObject(int value)
        {
            this.DataBytes = BitConverter.GetBytes(value);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataObject"/> class.
        /// </summary>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public DataObject(bool value)
        {
            this.DataBytes = BitConverter.GetBytes(value);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataObject"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        public DataObject(Image value)
        {
            this.DataBytes = DataConverter.FromImage(value);
        }
        #endregion

        #region Properties
        /// <summary>
        /// this will hold all the information related to 
        /// an object as a byte array
        /// </summary>
        public Stream Data { get; set; }

        /// <summary>
        /// this is the revision number of this object. 
        /// This will be initially assigned by the relevant 
        /// DataProvider and it should be able to interpret
        /// this later time to retrieve the same information
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// This is a name to the content of this object. This will typically be the name of the file 
        /// this object encapsulates. But it can be some other name other than a file name, especially
        /// when this object is holding some data from database. 
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Sometimes, the steam has to be consumed. In tht case data will be stored as a byte buffer
        /// </summary>
        public byte[] DataBytes { get; set; }
        #endregion

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (this.Data != null)
            {
                this.Data.Close();
                this.Data.Dispose();
            }
        }

        #endregion
    }

}

